<script>
import { mapGetters } from 'vuex'
import { cardStatusArray } from '@/constants'
import DialogForm from '@/views/car/monthCard/components/dialogForm/dialogForm.vue'
import CardDetail from '@/views/car/monthCard/components/cardDetail/cardDetail.vue'

export default {
  name: 'monthCard',
  components: { CardDetail, DialogForm },
  data() {
    return {
      /* 获取月卡列表的分页参数 */
      pageParams: { page: 1, pageSize: 10, carNumber: undefined, personName: undefined, cardStatus: null },
      /* 加载月卡的状态 */
      loading: false,
      /* 待删除月卡的id数组 */
      cardIdArray: [],
      cardStatusArray,
      /* 是否显示添加月卡、续费月卡、编辑月卡的对话框 */
      dialogVisible: false,
      /* 场景值 0添加月卡 1续费月卡 2编辑月卡 */
      scene: 0,
      /* 是否查看月卡 */
      showCard: false
    }
  },
  methods: {
    /* 获取月卡列表的回调函数 */
    async fetchCardList(pageParams) {
      try {
        this.loading = true
        await this.$store.dispatch('monthCardStore/fetchCardList', pageParams)
      } catch (error) {
        this.$message.error(error.message)
      } finally {
        this.loading = false
      }
    },
    /* 分页器size发生变化的回调函数 */
    handleSizeChange(pageSize) {
      this.pageParams.pageSize = pageSize
      this.fetchCardList(this.pageParams)
    },
    /* 分页器page发生变化的回调函数 */
    handleCurrentChange(page) {
      this.pageParams.page = page
      this.fetchCardList(this.pageParams)
    },
    /* 点击查询按钮的回调函数 */
    handleSearch() {
      this.pageParams.page = 1
      this.fetchCardList(this.pageParams)
    },
    /* 点击删除按钮的回调函数 */
    async handleDelete(row) {
      const confirm = await this.$confirm(`确定删除${row.personName}吗?是否继续`, '提示').catch(error => error)
      if (confirm === 'cancel') return
      try {
        const deleteResult = await this.$store.dispatch('monthCardStore/deleteCard', row.id)
        this.$message.success(deleteResult)
        this.fetchCardList(this.pageParams)
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    /* 点击批量删除月卡按钮的回调函数 */
    async handleBatchDelete() {
      try {
        const deleteResult = await this.$store.dispatch('monthCardStore/deleteCard', this.cardIdArray.join(','))
        this.$message.success(deleteResult)
        this.fetchCardList(this.pageParams)
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    handleAdd() {
      this.dialogVisible = true
      this.scene = 0
    },
    handleRenew(row) {
      this.dialogVisible = true
      this.scene = 1
      this.$nextTick(() => {
        this.$refs.dialogForm.findCard(row.id)
      })
    },
    handleEdit(row) {
      this.dialogVisible = true
      this.scene = 2
      this.$nextTick(() => {
        this.$refs.dialogForm.findCard(row.id)
      })
    },
    handleShow(row) {
      this.showCard = true
      this.$nextTick(() => {
        this.$refs.cardDetail.fetchCardDetail(row.id)
      })
    },
    /* 当表格选择项发生变化时会触发该事件 */
    selectionChange(selection) {
      this.cardIdArray = selection.map(item => item.id)
    },
    success() {
      this.dialogVisible = false
      this.fetchCardList(this.pageParams)
    }
  },
  computed: {
    ...mapGetters('monthCardStore', ['cardList', 'total']),
    dialogTitle() {
      if (this.scene === 0) {
        return '添加月卡'
      } else if (this.scene === 1) {
        return '续费月卡'
      } else {
        return '编辑月卡'
      }
    }
  },
  mounted() {
    this.fetchCardList(this.pageParams)
  }
}
</script>

<template>
  <div class="monthCard">
    <el-card shadow="never">
      <el-form inline :model="pageParams">
        <el-form-item label="车牌号码">
          <el-input size="small" placeholder="请输入车牌号码" v-model="pageParams.carNumber" :disabled="showCard"></el-input>
        </el-form-item>
        <el-form-item label="车主姓名">
          <el-input size="small" placeholder="请输入车主姓名" v-model="pageParams.personName" :disabled="showCard"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select size="small" v-model="pageParams.cardStatus" placeholder="请选择状态" :disabled="showCard">
            <el-option v-for="(item) in cardStatusArray" :value="item.value" :label="item.label" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" icon="el-icon-search" @click="handleSearch" :disabled="showCard">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card shadow="never" v-if="!showCard">
      <el-row>
        <el-col :span="18">
          <el-button type="primary" size="small" icon="el-icon-plus" @click="handleAdd">添加月卡</el-button>
          <el-button type="danger" size="small" icon="el-icon-delete" @click="handleBatchDelete">批量删除</el-button>
        </el-col>
        <el-col :span="6">
          <el-alert title="本园区共计 1530 个车位，月卡用户 0 人，车位占有率 0.00%" effect="dark" :closable="false" show-icon></el-alert>
        </el-col>
      </el-row>
      <el-table size="small" border stripe :data="cardList" v-loading="loading" element-loading-text="拼命加载中" @selection-change="selectionChange">
        <el-table-column type="selection" align="center"></el-table-column>
        <el-table-column label="序号" type="index" align="center"></el-table-column>
        <el-table-column label="车主名称" prop="personName" align="center" width="200"></el-table-column>
        <el-table-column label="联系方式" prop="phoneNumber" align="center" width="200"></el-table-column>
        <el-table-column label="车牌号码" prop="carNumber" align="center" width="200"></el-table-column>
        <el-table-column label="车辆品牌" prop="carBrand" align="center" width="300"></el-table-column>
        <el-table-column label="剩余有效天数" prop="totalEffectiveDate" align="center" width="300"></el-table-column>
        <el-table-column label="状态" prop="cardStatus" align="center" width="300">
          <template #default="{row}">{{ row.cardStatus === 0 ? '可用' : '过期' }}</template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="{row}">
            <el-button type="success" size="small" icon="el-icon-plus" @click="handleRenew(row)">续费月卡</el-button>
            <el-button type="info" size="small" icon="el-icon-view" @click="handleShow(row)">查看月卡</el-button>
            <el-button type="warning" size="small" icon="el-icon-edit" @click="handleEdit(row)">编辑月卡</el-button>
            <el-button type="danger" size="small" icon="el-icon-delete" @click="handleDelete(row)">删除月卡</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageParams.page" :page-sizes="[10, 20, 30, 40]" :page-size="pageParams.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" background></el-pagination>
    </el-card>
    <card-detail v-else ref="cardDetail" @back="showCard=false"></card-detail>
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="35%" @close="$refs.dialogForm.resetFormData()">
      <dialog-form ref="dialogForm" :scene="scene" @success="success"></dialog-form>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">
.monthCard {
  padding: 10px 0;

  .el-card {
    margin-bottom: 10px;
  }

  .el-table {
    margin: 10px 0;
  }

  .el-pagination {
    display: flex;
    justify-content: center;
  }
}
</style>
